/*
 * @Author: asushiye
 * @Date: 2020-07-07 09:41:13
 * @Commit: wechatkey, notice, message
 */

import { Injectable } from '@angular/core';
import { dateTimePickerUtil as dateUtil, getTimeDistance } from '@delon/util';

@Injectable({
  providedIn: 'root'
})
export class AfDateService {
  readonly long_date_format: string = 'yyyy-MM-dd HH:mm:ss';
  readonly short_date_format: string = 'yyyy-MM-dd';
  readonly short_time_format: string = 'HH:mm:ss';

  constructor() {}

  public format(d: number | Date, formatString?: string) {
    return dateUtil.format(d, formatString);
  }

  public get tsToday() {
    return dateUtil.format(new Date(), this.long_date_format);
  }

  public get ssToday() {
    return dateUtil.format(new Date(), this.short_date_format);
  }

  public get dToday() {
    return dateUtil.removeTime(new Date());
  }

  //  today、-today 今天或昨天  week、-week 本周或上周 month、-month 本月或上月 year、-year 今年或去年
  //返回： [ Date, Date]
  public getWeekDate() {
    return getTimeDistance('week');
  }

  //获取 7 天
  public getSeventDate() {
    let date = new Date();
    let lastDate = date.setDate(date.getDate() - 7);
    return [lastDate, date];
  }

  // 获取 yyyy-MM-dd 00:00:00 数据，配合springboot 查询使用
  public formatLongDate(value: Date) {
    return dateUtil.format(dateUtil.removeTime(value), this.long_date_format);
  }

  public formatShortDate(value: Date) {
    return dateUtil.format(value, this.short_date_format);
  }

  // 配合日期选择器使用 nz-range-picker
  public formatRangeDate(value: any[]) {
    let shortDate: any[] = [];
    if (value != null && value.length >= 1) {
      for (let index = 0; index < value.length; index++) {
        const sDate = this.formatLongDate(value[index]);
        shortDate[index] = sDate;
      }
    } else {
      shortDate = ['', ''];
    }
    return shortDate;
  }
}
